#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.
#
# This version is for a hypothetical package that can build a kernel modules
# architecture-dependant package via make-kpkg, as well as an
# architecture-independent module source package, and other packages
# either dep/indep for things like common files or userspace components
# needed for the kernel modules.

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1




# some default definitions, important!
#
# Name of the source package
psource:=#TYPE#-source

# The short upstream name, used for the module source directory
sname:=#TYPE#

### KERNEL SETUP
### Setup the stuff needed for making kernel module packages
### taken from /usr/share/kernel-package/sample.module.rules

# prefix of the target package name
PACKAGE=#TYPE#-modules
# modifieable for experiments or debugging m-a
MA_DIR ?= /usr/share/modass
# load generic variable handling
-include $(MA_DIR)/include/generic.make
# load default rules, including kdist, kdist_image, ...
-include $(MA_DIR)/include/common-rules.make

userpackage:=#TYPE#-user

pdkms:=#TYPE#-dkms

develpackage:=#TYPE#-dev
examplespackage:=#TYPE#-examples
get_build_config = $(patsubst pkg.$(sname).%,%,\
                     $(filter pkg.$(sname).$(1),$(DEB_BUILD_PROFILES)))

BUILD_PROFILE=$(patsubst profile-%,%,$(call get_build_config,profile-%))
ifeq ($(shell test $(words $(BUILD_PROFILE)) -gt 1; echo $$?),0)
  $(error Too many onload build profiles, profile-*, provided. Only one allowed!)
endif

BUILD_FLAGS=
ifneq ($(BUILD_PROFILE),)
  $(info Using Onload build profile $(BUILD_PROFILE))
  BUILD_FLAGS+=--build-profile $(BUILD_PROFILE)
endif
ifneq ($(call get_build_config,debug),)
  BUILD_FLAGS+=--debug
endif
ifneq ($(call get_build_config,noefct),)
  BUILD_FLAGS+=--no-efct
endif
ifneq ($(call get_build_config,havesdci),)
  BUILD_FLAGS+=--have-sdci
endif

SETUID=
ifneq ($(call get_build_config,setuid),)
  SETUID=--setuid
endif

DOPACKAGES := $(shell dh_listpackages)

# module assistant calculates all needed things for us and sets
# following variables:
# KSRC (kernel source directory), KVERS (kernel version string), KDREV
# (revision of the Debian kernel-image package), CC (the correct
# compiler), VERSION (the final package version string), PKGNAME (full
# package name with KVERS included), DEB_DESTDIR (path to store DEBs)

# The kdist_config target is called by make-kpkg modules_config and
# by kdist* rules by dependency. It should configure the module so it is
# ready for compilation (mostly useful for calling configure).
# prep-deb-files from module-assistant creates the neccessary debian/ files
kdist_config: prep-deb-files

# the kdist_clean target is called by make-kpkg modules_clean and from
# kdist* rules. It is responsible for cleaning up any changes that have
# been made by the other kdist_commands (except for the .deb files created)
kdist_clean: clean
	dh_testdir
	dh_clean
	rm -rf build

### end  KERNEL SETUP

configure: configure-stamp
configure-stamp:
	dh_testdir

	# Add here commands to configure the package.

	touch configure-stamp

override_dh_auto_build: build-arch-stamp
build-arch: configure-stamp  build-arch-stamp
build-arch-stamp:
	dh_testdir

ifneq (,$(filter #TYPE#-user,$(DOPACKAGES)))
	scripts/onload_build $(BUILD_FLAGS) --user
else
ifneq (,$(filter #TYPE#-dev,$(DOPACKAGES)))
	mkdir build
endif
endif

	touch $@

#k = $(shell echo $(KVERS) | grep -q ^2.6 && echo k)

# the binary-modules rule is invoked by module-assistant while processing the
# kdist* targets. It is called by module-assistant or make-kpkg and *not*
# during a normal build
binary-modules:
	dh_testroot
	dh_prep
	dh_installdirs lib/modules/$(KVERS)/misc usr/share/initramfs-tools/hooks

	# Make dh_installmodules(1) trigger update-initramfs
	cp scripts/onload_misc/onload_initramfs_hook \
		$(CURDIR)/debian/$(PKGNAME)/usr/share/initramfs-tools/hooks/onload

	# Build the module
	scripts/onload_build $(BUILD_FLAGS) --kernel

	# Install the module
	i_prefix=$(CURDIR)/debian/$(PKGNAME) scripts/onload_install --verbose \
		--packaged --kernelfiles $(BUILD_FLAGS) $(SET_UID)

	dh_installdocs
	dh_installchangelogs
	dh_installmodules
	dh_installinitramfs
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol -- -v$(VERSION)
	dh_md5sums
	dh_builddeb --destdir=$(DEB_DESTDIR)
	dh_prep

build-indep:  configure-stamp build-indep-stamp
build-indep-stamp:
	dh_testdir

	# Add here command to compile/build the arch indep package.
	# It's ok not to do anything here, if you don't need to build
	#  anything for this package.
	#docbook-to-man debian/#TYPE#.sgml > #TYPE#.1

	touch $@

build: build-arch build-indep

clean:
	dh_testdir
	rm -f build-arch-stamp build-indep-stamp configure-stamp

	# Add here commands to clean up after the build process.
	rm -rf build

	dh_clean

install: DH_OPTIONS=
install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

ifneq (,$(filter #TYPE#-source,$(DOPACKAGES)))
	# Create the directories to install the source into
	dh_installdirs -p$(psource)  usr/src/modules/$(sname)/debian

	# Copy only the driver source to the proper location
	cp Makefile  debian/$(psource)/usr/src/modules/$(sname)
	cp -r src  debian/$(psource)/usr/src/modules/$(sname)
	cp -r scripts  debian/$(psource)/usr/src/modules/$(sname)
	cp -r mk  debian/$(psource)/usr/src/modules/$(sname)
	# Copy the needed debian/ pieces to the proper location
	cp debian/*modules.in* \
		debian/$(psource)/usr/src/modules/$(sname)/debian
	cp debian/control \
		debian/$(psource)/usr/src/modules/$(sname)/debian
	cp debian/rules debian/changelog debian/copyright \
		debian/compat debian/$(psource)/usr/src/modules/$(sname)/debian/
	cd debian/$(psource)/usr/src && tar c modules | bzip2 -9 > $(sname).tar.bz2 && rm -rf modules
endif

ifneq (,$(filter #TYPE#-dkms,$(DOPACKAGES)))
	echo 'MAKE[0]+=" $(BUILD_FLAGS)"' > dkms_override.conf
	install -D -m 644 dkms_override.conf debian/$(pdkms)/etc/dkms/$(sname).conf
	dh_installdirs -p$(pdkms) usr/src/
	tar xf ../#TYPE#_#VERSION#.orig.tar.gz -C debian/$(pdkms)/usr/src/
endif

	# Add here commands to install the package into debian/#TYPE#.

	dh_install

# Build architecture-independent files here.
# Pass -i to all debhelper commands in this target to reduce clutter.
binary-indep: build install
	dh_testdir -i
	dh_testroot -i
	dh_installchangelogs ChangeLog -i
	dh_installdocs -i
	dh_installexamples -i
#	dh_install -i
#	dh_installmenu -i
#	dh_installdebconf -i
#	dh_installlogrotate -i
#	dh_installemacsen -i
#	dh_installpam -i
#	dh_installmime -i
#	dh_installinit -i
#	dh_installcron -i
#	dh_installinfo -i
	dh_installman -i
	dh_link -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
#	dh_perl -i
#	dh_makeshlibs -i
	dh_installdeb -i
	dh_shlibdeps -i

ifneq (,$(filter #TYPE#-dev,$(DOPACKAGES)))
	dh_installdirs -p$(develpackage) usr/include/
	i_prefix=$(CURDIR)/debian/$(develpackage) scripts/onload_install \
		--verbose --packaged --headers --nobaseheaders
endif
ifneq (,$(filter #TYPE#-examples,$(DOPACKAGES)))
	dh_installdirs -p$(examplespackage) usr/share/doc/onload/examples
	i_prefix=$(CURDIR)/debian/$(examplespackage) scripts/onload_install \
		--verbose --packaged --examples
endif
# do these after the optional components have been added
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build install
	dh_lintian
	dh_testdir -a
	dh_testroot -a
	dh_installsysusers -a --name=onload || dh_sysuser -a onload_cplane home=/run/openonload
#	dh_installdebconf -a
	dh_installdocs -a
	dh_installexamples -a
	dh_installmenu -a
#	dh_installlogrotate -a
#	dh_installemacsen -a
#	dh_installpam -a
#	dh_installmime -a
#	dh_installinit -a
	dh_installcron -a
#	dh_installman -a
	dh_installinfo -a
	dh_installchangelogs ChangeLog -a
	dh_strip -a
	dh_link -a
	dh_compress -a
	dh_fixperms -a
	dh_makeshlibs -a
	dh_installdeb -a
#	dh_perl -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a

ifneq (,$(filter #TYPE#-user,$(DOPACKAGES)))
	# Create the directories to install the source into
	dh_installdirs -p$(userpackage) lib/onload

	i_prefix=$(CURDIR)/debian/$(userpackage) scripts/onload_install --verbose \
		--packaged --userfiles --modprobe --modulesloadd --python-layout=deb \
		$(BUILD_FLAGS) $(SETUID)
endif

	dh_builddeb -a

binary: binary-indep binary-arch

.PHONY: build clean binary-indep binary-arch binary install configure binary-modules kdist kdist_configure kdist_image kdist_clean
